//
//  CommentListModel.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/17.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "CommentListModel.h"
#import "NetworkingTool.h"
#import "CommentModel.h"
#import <MJExtension/MJExtension.h>
#import "JKProgressHUD.h"

@implementation CommentListModel

/** 每次最多可获取评论数据数量 */
static int maxCount = 20;
static NSMutableArray *_longCmts;
static NSMutableArray *_shortCmts;

+ (void)initialize{
    _longCmts = [NSMutableArray array];
    _shortCmts = [NSMutableArray array];
}

/** 加载 长评论 */
+ (void)loadLongCommentsWithNewsID:(int)newsID finished:(void(^)(NSMutableArray *arr))finished{
    
    NSString *urlStr = [NSString stringWithFormat:@"story/%d/long-comments", newsID];
    [[NetworkingTool sharedNetworkingTool] loadDataWithUrl:urlStr parameters:nil finishedBlock:^(id responseObj, NSError *error) {
        if (error) return;
        
        NSMutableArray *longCmts = [CommentModel mj_objectArrayWithKeyValuesArray:responseObj[@"comments"]];
        !finished ? : finished(longCmts);
    }];
}

/**
 加载更多的长评论
 
 - parameter newsId:   文章的ID
 - parameter beforeId: 长评论的最后一个ID
 - parameter finished: 完成的回调
 */
+ (void)loadMoreLongCommentsWithNewsID:(int)newsID beforeID:(int)beforeID finished:(void(^)(NSMutableArray *arr))finished{
    
    NSString *urlStr = [NSString stringWithFormat:@"story/%d/long-comments/before/%d", newsID, beforeID];
    [[NetworkingTool sharedNetworkingTool] loadDataWithUrl:urlStr parameters:nil finishedBlock:^(id responseObj, NSError *error) {
        if (error) return;
        
        NSMutableArray *moreLongCmts = [CommentModel mj_objectArrayWithKeyValuesArray:responseObj[@"comments"]];
        !finished ? : finished(moreLongCmts);
    }];
}

/** 加载 短评论 */
+ (void)loadShortCommentsWithNewsID:(int)newsID finished:(void(^)(NSMutableArray *arr))finished{
    
    NSString *urlStr = [NSString stringWithFormat:@"story/%d/short-comments", newsID];
    [[NetworkingTool sharedNetworkingTool] loadDataWithUrl:urlStr parameters:nil finishedBlock:^(id responseObj, NSError *error) {
        if (error) return;
        
        NSMutableArray *longCmts = [CommentModel mj_objectArrayWithKeyValuesArray:responseObj[@"comments"]];
        !finished ? : finished(longCmts);
    }];
}

/** 加载更多的短评论 */
+ (void)loadMoreShortCommentsWithNewsID:(int)newsID beforeID:(int)beforeID finished:(void(^)(NSMutableArray *arr))finished{
    
    NSString *urlStr = [NSString stringWithFormat:@"story/%d/short-comments/before/%d", newsID, beforeID];
    [[NetworkingTool sharedNetworkingTool] loadDataWithUrl:urlStr parameters:nil finishedBlock:^(id responseObj, NSError *error) {
        if (error) return;
        
        NSMutableArray *moreLongCmts = [CommentModel mj_objectArrayWithKeyValuesArray:responseObj[@"comments"]];
        !finished ? : finished(moreLongCmts);
    }];
}

/** 获取所有长评论数据 */
+ (void)getAllLongCommentsWithNewsID:(int)newsID complete:(void(^)(NSMutableArray *longCmts))complete{
    [_longCmts removeAllObjects];
    
    [CommentListModel loadLongCommentsWithNewsID:newsID finished:^(NSMutableArray *arr) {
        _longCmts = arr;
        
        // 数据不足20条，说明已经获取完毕，直接返回
        if (arr.count < maxCount) {
            complete(_longCmts);
            return;
        }
        
        // 数据 == 20条，说明可能还有数据
        if (arr.count == maxCount) {
            CommentModel *lastCmt = _longCmts.lastObject;
            [self getMoreLongCommentsWithNewsID:newsID beforeID:lastCmt.ID done:complete];
        }
    }];
}

/** 递归获取所有长评论数据 */
+ (void)getMoreLongCommentsWithNewsID:(int)newsID beforeID:(int)beforeID done:(void(^)(NSMutableArray *longCmts))done{
    
    [CommentListModel loadMoreLongCommentsWithNewsID:newsID beforeID:beforeID finished:^(NSMutableArray *arr) {
        
        // 获取数据为0，说明已经获取完毕
        if (arr.count == 0) {
            done(_longCmts);
            return;
            
        }else if (arr.count < maxCount){// 获取数据为小于，说明已经获取完毕，添加到数组
            [_longCmts addObjectsFromArray:arr];
            done(_longCmts);
            return;
            
        }else if (arr.count == maxCount){// 数据 == 20条，说明可能还有数据
            [_longCmts addObjectsFromArray:arr];
            CommentModel *lastCmt = _longCmts.lastObject;
            [self getMoreLongCommentsWithNewsID:newsID beforeID:lastCmt.ID done:done];
            
        }else{
            return;
        }
    }];
}

/** 获取所有短评论数据 */
+ (void)getAllShortCommentsWithNewsID:(int)newsID complete:(void(^)(NSMutableArray *shortCmts))complete{
    [_shortCmts removeAllObjects];
    [CommentListModel loadShortCommentsWithNewsID:newsID finished:^(NSMutableArray *arr) {
        
        _shortCmts = arr;
        
        // 数据不足20条，说明已经获取完毕，直接返回
        if (arr.count < maxCount) {
            complete(_shortCmts);
            return;
        }
        
        // 数据 == 20条，说明可能还有数据
        if (arr.count == maxCount) {
            CommentModel *lastCmt = _shortCmts.lastObject;
            [self getMoreShortCommentsWithNewsID:newsID beforeID:lastCmt.ID done:complete];
        }
    }];
}

/** 递归获取所有短评论数据 */
+ (void)getMoreShortCommentsWithNewsID:(int)newsID beforeID:(int)beforeID done:(void(^)(NSMutableArray *shortCmts))done{
    
    [CommentListModel loadMoreShortCommentsWithNewsID:newsID beforeID:beforeID finished:^(NSMutableArray *arr) {
        
        // 获取数据为0，说明已经获取完毕
        if (arr.count == 0) {
            done(_shortCmts);
            return;
            
        }else if (arr.count < maxCount){// 获取数据为小于，说明已经获取完毕，添加到数组
            [_shortCmts addObjectsFromArray:arr];
            done(_shortCmts);
            return;
            
        }else if (arr.count == maxCount){// 数据 == 20条，说明可能还有数据
            [_shortCmts addObjectsFromArray:arr];
            CommentModel *lastCmt = _shortCmts.lastObject;
            [self getMoreShortCommentsWithNewsID:newsID beforeID:lastCmt.ID done:done];
            
        }else{
            return;
        }
    }];
}
@end
